package com.gj.demojuc.chap08;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 工具类Executors创建线程池执行任务
 */
public class ExecutorsDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建单一线程的线程池对象
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        //调用线程池对象执行任务
        //execute只适用于runnable
        //submit可以传callable和runnable，有返回结果
        Future<String> future = executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "任务执行");
            return "atguigu";
        });
        //获取子线程执行结果
        String result = future.get();
        System.out.println(Thread.currentThread().getName() + result);
    }
}
